package com.xjrsoft.module.customer.erp_model.erpPurchaseRequsetInfo.controller;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.helper.DbHelper;
import com.xjrsoft.common.helper.DbHelperFactory;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.page.ConventPage;

import java.sql.SQLException;
import java.util.List;
import com.xjrsoft.common.utils.DataTransUtil;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.module.base.service.IXjrBaseCodeRuleService;
import com.xjrsoft.module.customer.erp_model.erpPurchaseRequsetInfo.dto.ErpPurchaserequsetDto;
import com.xjrsoft.module.customer.erp_model.erpPurchaseRequsetInfo.dto.SaveErpPurchaserequsetFormDataDto;
import com.xjrsoft.module.customer.erp_model.erpPurchaseRequsetInfo.vo.ErpPurchaserequsetFormDataVo;
import com.xjrsoft.module.customer.erp_model.erpPurchaseRequsetInfo.vo.ErpPurchaserequsetListVo;
import com.xjrsoft.module.customer.erp_model.erpPurchaseRequsetInfo.dto.ErpPurchaserequsetListDto;
import com.xjrsoft.module.customer.erp_model.erpPurchaseRequsetInfo.vo.ErpPurchaserequsetentryVo;
import com.xjrsoft.module.customer.erp_model.erpPurchaseRequsetInfo.entity.ErpPurchaserequsetentry;
import com.xjrsoft.module.customer.erp_model.erpPurchaseRequsetInfo.dto.ErpPurchaserequsetentryDto;

import java.util.Map;
import java.util.stream.Collectors;
import java.io.File;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.xjrsoft.core.tool.utils.CollectionUtil;
import com.xjrsoft.core.tool.utils.IoUtil;
import com.xjrsoft.module.excel.entity.XjrExcelImport;
import com.xjrsoft.module.excel.factory.ParserFactory;
import com.xjrsoft.module.excel.parser.ImportDataParser;
import com.xjrsoft.module.excel.service.IXjrExcelImportService;
import org.springframework.web.multipart.MultipartFile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

import com.xjrsoft.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import com.xjrsoft.module.customer.erp_model.erpPurchaseRequsetInfo.entity.ErpPurchaserequset;
import com.xjrsoft.module.customer.erp_model.erpPurchaseRequsetInfo.vo.ErpPurchaserequsetVo;
import com.xjrsoft.module.customer.erp_model.erpPurchaseRequsetInfo.service.IErpPurchaserequsetService;

/**
 *  控制器
 *
 * @author pp
 * @since 2021-09-24
 */
@RestController
@AllArgsConstructor
@RequestMapping("/erp_purchaserequsetinfo")
@Api(value = "采购申请", tags = "采购申请接口")
public class ErpPurchaserequsetController {

	private final IXjrExcelImportService importService;

	private final ObjectMapper objectMapper;

	private final IXjrBaseCodeRuleService codeRuleService;

	private final IErpPurchaserequsetService erpPurchaserequsetService;


	/**
	 * 详情
	 */
	@GetMapping("/{id}")
	@ApiOperation(value = "详情", notes = "传入erpPurchaserequset")
	public Response<ErpPurchaserequsetFormDataVo> getFormData(@PathVariable String id) {
		ErpPurchaserequsetFormDataVo formDataVo = new ErpPurchaserequsetFormDataVo();
		// 主表数据
		ErpPurchaserequset erpPurchaserequset = erpPurchaserequsetService.getById(id);
		formDataVo.setErpPurchaserequsetVo(BeanUtil.copy(erpPurchaserequset, ErpPurchaserequsetVo.class));
		// 从表数据
		formDataVo.setErpPurchaserequsetentryVoList(BeanUtil.copyList(erpPurchaserequsetService.getErpPurchaserequsetentryByParentId(id), ErpPurchaserequsetentryVo.class));
		return Response.ok(formDataVo);
	}


	@GetMapping("/unit")
	@ApiOperation(value = "获取单位")
	public Response<List<Map<String, Object>>> getUtil() throws SQLException, ClassNotFoundException {
		DbHelper dbHelper = DbHelperFactory.getDbHelper(DbType.MYSQL, "localDB");
		return Response.ok(dbHelper.getTableData("select c.F_Id,c.F_FullName from erp_unitconvert c", ""));
	}

	/**
	 * 自定义分页
	 */
	@GetMapping
	@ApiOperation(value = "分页", notes = "传入erpPurchaserequset")
	public Response<PageOutput<ErpPurchaserequsetListVo>> getPageList(ErpPurchaserequsetListDto listDto) {
		IPage<ErpPurchaserequset> page = erpPurchaserequsetService.getPageList(listDto);
		List<ErpPurchaserequsetListVo> records = BeanUtil.copyList(page.getRecords(), ErpPurchaserequsetListVo.class);
		// 转换列表数据
		DataTransUtil.transListShowData(records);
		return Response.ok(ConventPage.getPageOutput(page.getTotal(), records));
	}

	/**
	 * 新增
	 */
	@PostMapping
	@ApiOperation(value = "新增", notes = "传入erpPurchaserequset")
	public Response save(@RequestBody SaveErpPurchaserequsetFormDataDto formDto) {
		ErpPurchaserequsetDto erpPurchaserequsetDto = formDto.getErpPurchaserequsetDto();
		ErpPurchaserequset erpPurchaserequset = BeanUtil.copy(erpPurchaserequsetDto, ErpPurchaserequset.class);
		List<ErpPurchaserequsetentryDto> erpPurchaserequsetentryDtoList = formDto.getErpPurchaserequsetentryDto();
		List<ErpPurchaserequsetentry> erpPurchaserequsetentryList = BeanUtil.copyList(erpPurchaserequsetentryDtoList, ErpPurchaserequsetentry.class);
		String dataId = erpPurchaserequsetService.addErpPurchaserequset(erpPurchaserequset, erpPurchaserequsetentryList);
		if (dataId != null) {
			codeRuleService.useEncode("DocunNumber");
		}
		return dataId == null ? Response.notOk() : Response.ok(dataId);
	}

	/**
	 * 修改
	 */
	@PutMapping("/{id}")
	@ApiOperation(value = "修改", notes = "传入erpPurchaserequset")
	public Response update(@PathVariable String id, @RequestBody SaveErpPurchaserequsetFormDataDto formDto) {
		ErpPurchaserequsetDto erpPurchaserequsetDto = formDto.getErpPurchaserequsetDto();
		ErpPurchaserequset erpPurchaserequset = BeanUtil.copy(erpPurchaserequsetDto, ErpPurchaserequset.class);
		List<ErpPurchaserequsetentryDto> erpPurchaserequsetentryDtoList = formDto.getErpPurchaserequsetentryDto();
		List<ErpPurchaserequsetentry> erpPurchaserequsetentryList = BeanUtil.copyList(erpPurchaserequsetentryDtoList, ErpPurchaserequsetentry.class);
		return Response.status(erpPurchaserequsetService.updateErpPurchaserequset(id, erpPurchaserequset, erpPurchaserequsetentryList));
	}


	/**
	 * 删除
	 */
	@DeleteMapping("/{ids}")
	@ApiOperation(value = "删除", notes = "传入ids")
	public Response remove(@ApiParam(value = "主键集合", required = true) @PathVariable String ids) {
		return Response.status(erpPurchaserequsetService.removeByIds(Func.toStrList(ids)));
	}

	/**
	 * excel批量导入数据
	 */
	@PostMapping("/import")
	@ApiOperation(value = "excel批量导入数据", notes = "传入菜单id")
	public Response importData(@RequestParam("F_ModuleId") String moduleId, MultipartFile file) {
		XjrExcelImport excelImport = importService.getByModuleId(moduleId);
		ImportDataParser parser = ParserFactory.getImportDataParser();
		File excelFile = IoUtil.toFile(file);
		List<Map<String, Object>> recordList = parser.parseDataToMap(excelImport, excelFile);
		if (CollectionUtil.isNotEmpty(recordList)) {
			List<ErpPurchaserequsetDto> entityDtoList = recordList.stream().map(record -> {
				try {
					return objectMapper.readValue(objectMapper.writeValueAsString(record), ErpPurchaserequsetDto.class);
				} catch (JsonProcessingException e) {
				}
				return null;
			}).collect(Collectors.toList());
			List<ErpPurchaserequset> entities = BeanUtil.copyList(entityDtoList, ErpPurchaserequset.class);
			return Response.status(erpPurchaserequsetService.saveBatch(entities));
		}
		return Response.notOk();
	}

    @PostMapping("/examine/{id}")
    @ApiOperation(value = "审核", notes = "传入id")
    public Response examine(@ApiParam(value = "主键集合", required = true) @PathVariable String id,@RequestBody SaveErpPurchaserequsetFormDataDto formDto) {
		String dataId = erpPurchaserequsetService.examine(id, formDto);
		return dataId == null ? Response.notOk() : Response.ok(dataId);
    }
}
